package com.uduemc.biso.module.basic.service.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.github.pagehelper.page.PageMethod;
import com.uduemc.biso.core.extities.center.Site;
import com.uduemc.biso.module.basic.service.mapper.SiteMapper;
import com.uduemc.biso.module.basic.service.service.SiteService;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service
public class SiteServiceImpl implements SiteService {

	@Autowired
	private SiteMapper siteMapper;

	@Override
	public Site findOne(Long id) {
		return siteMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<Site> findByHostId(Long hostId) {
		Example example = new Example(Site.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("hostId", hostId);
		return siteMapper.selectByExample(example);
	}

	@Override
	public Site insert(Site site) {
		siteMapper.insertSelective(site);
		return findOne(site.getId());
	}

	@Override
	public Site updateById(Site site) {
		siteMapper.updateByPrimaryKeySelective(site);
		return findOne(site.getId());
	}

	@Override
	public List<Site> findAll(Pageable pageable) {
		int pageNumber = pageable.getPageNumber();
		int pageSize = pageable.getPageSize();
		PageMethod.startPage(pageNumber, pageSize);
		return siteMapper.selectAll();
	}

	@Override
	public void deleteById(Long id) {
		siteMapper.deleteByPrimaryKey(id);
	}

}
